#/bin/bash
#memory 1G+
Url=http://mirror.cnop.net
echo "" && echo -e "\033[32m Please input install path( Default /usr/local/mysql ): \033[0m"
read Path
P='$PATH'
if [ -z "$Path" ]; then
  echo -e "\033[32m  Using the default path /usr/local/mysql: \033[0m"
  Path=/usr/local/mysql
  echo $Path
else
  echo -e "\033[32m ....... Your input $Path ........ \033[0m"
  echo $Path
fi

echo "Install lib:"
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget $Url/lib/devtoolset/rhscl-devtoolset-3-epel-7-x86_64-1-2.noarch.rpm
rpm -ivh rhscl-devtoolset-3-epel-7-x86_64-1-2.noarch.rpm
yum install -y epel-release centos-release-scl-rh centos-release-scl unzip wget devtoolset-3-gcc devtoolset-3-gcc-c++ devtoolset-3-gdb cmake  openssl-devel bison gmock ncurses ncurses-* autoconf  net-tools
echo "Create mysql user:"
groupadd mysql && useradd -r -g mysql mysql

echo "" && echo -e "\033[32m Start install : \033[0m"
wget $Url/mysql/alisql/AliSQL-5.6.32.zip
unzip AliSQL-5.6.32.zip && cd AliSQL-master && source /opt/rh/devtoolset-3/enable && mkdir source_downloads && cd source_downloads && wget $Url/gmock/gmock-1.6.0.zip && cd ..

cmake \
-DCMAKE_INSTALL_PREFIX=$Path \
-DMYSQL_UNIX_ADDR=$Path/data/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=$Path/data \
-DMYSQL_TCP_PORT=3306 \
-DENABLE_DOWNLOADS=1

make && make install
chown -R mysql.mysql $Path
echo never > /sys/kernel/mm/transparent_hugepage/enabled
chmod +x ./scripts/mysql_install_db
./scripts/mysql_install_db  --user=mysql  --basedir=$Path --datadir=$Path/data

cat >/etc/my.cnf<<EOF

[client]
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = $Path/data
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1000M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
max_connections = 1000
max_connect_errors = 1000
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id	= 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = $Path/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir =  $Path/data
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

EOF

cp  support-files/mysql.server  /etc/init.d/mysqld
echo "
PATH=$Path/bin:$Path/lib:$P
export PATH
" >>/etc/profile
source /etc/profile
#sed -i "s/\/usr\/local\/alisql/\/usr\/local\/aaa/g" /etc/profile
ln -s $Path/bin/mysql /usr/bin
echo "" && echo -e "\033[32m Starting: \033[0m"
systemctl daemon-reload
chmod +x $Path/bin/mysql && chmod +x /etc/init.d/mysqld
service mysqld start
chkconfig --level 35 mysqld on
netstat -tulnp | grep 3306

echo "" && echo -e "\033[32m Mysql password: \033[0m"
read Pw
echo "" && echo -e "\033[32m Mysql old password( Default empty, please press enter ): \033[0m"
mysqladmin -uroot  -p  password $Pw
source /etc/profile
echo "" && echo -e "\033[32m Mysql password:$Pw \033[0m"
echo "" && echo -e "\033[32m Done \033[0m"
